<?php

namespace App\Http\Controllers\Common;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Lixunguan\Yuntongxun\Sdk as Yuntongxun;

class CommonphoneCodeApi extends Controller
{	
	public function json($code, $msg, $data = [])
    {
        return response()->json([
        'status' => $code,
        'msg' => $msg,
        'data' => $data,
        ]);
    }

	/**
	 * [sendPhoneMsg 生成短信验证码]
	 * @author xiejunjie <[402649930@qq.com]>
	 * @param  [integer]  $phone      [该用户验证手机号码]
	 * @param  [type]  $code       [发送该用户手机验证码]
	 * @param  integer $templateId [验证码模板]
	 */
    public function sendPhoneMsg(Request $request, $phone, $templateId = 1) 
    {	

    	$phone = $request->input('phone');
    	$code = mt_rand(1000,9999);

    	//将验证码存到session
     	$request->session()->put('phonecode', $code);

    	$sdk = new Yuntongxun(env('RONG_APP_ID'), env('RONG_ACCOUNT_SID'), env('RONG_AUTH_TOKEN'));

		// 1是短信模板， 1858842345为手机号, 第二个参数为发送的短信验证码
		$sms = $sdk->sendTemplateSMS($phone, array($code), $templateId);

		//var_dump($sms);
		if ($sms->statusCode == '000000') {

			return $this->json('000000', '发送成功');
		}
    }

	//验证码验证
	public  static function CheckphoneCode (Request $request, $formCodeName = 'phonecode')
	{
	   $userCode = $request->input($formCodeName);
	   if ( $request->session()->get('phonecode') == $userCode ) {
	        return   true;
	   }
	        return false;
	}
}
